import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

//------------------------Vuex配置缓存------------------------

export default new Vuex.Store({

    state:{
        baseUrl:'http://localhost:8084/',
        baseUrlApi:'http://localhost:8084/api/',
        // baseUrl:'http://192.168.0.123:8085/',
        // baseUrlApi:'http://192.168.0.123:8085/api/',
        // ws:'',
        //TokenKey和Token
        tokenKey:'Authorization',
        token:sessionStorage.getItem('Authorization')||'',
        //用户IDKey和用户ID
        userIdKey:'UserId',
        userId:sessionStorage.getItem('UserId')||'',
        //用户信息Key和用户信息
        userInfoKey:'UserInfo',
        userInfo:JSON.parse(sessionStorage.getItem('UserInfo'))||null,
        //当前激活的菜单键和当前激活的菜单
        currentActiveMenuKey:'CurrentActiveMenu',
        currentActiveMenu:sessionStorage.getItem('CurrentActiveMenu')||''
    },
    mutations:{
        //缓存Token
        setToken(state, token) {
            state.token = token
            sessionStorage.setItem(state.tokenKey,token)
        },
        //缓存用户ID
        setUserId(state,userId){
            state.userId = userId
            sessionStorage.setItem(state.userIdKey,userId)
        },
        //缓存用户信息
        setUserInfo(state,userInfo){
            state.userInfo = userInfo
            let userInfoStr = JSON.stringify(userInfo)
            if(userInfoStr){
                sessionStorage.setItem(state.userInfoKey,userInfoStr)
            }
        },
        //缓存当前激活的菜单
        setCurrentActiveMenu(state,currentActiveMenu){
            state.currentActiveMenu = currentActiveMenu
            sessionStorage.setItem(state.currentActiveMenuKey,currentActiveMenu)
        },
        //清空缓存
        clearStore(state) {
            state.token = ''
            state.userId = ''
            state.userInfo = null
            state.currentActiveMenu = ''
            sessionStorage.removeItem(state.tokenKey)
            sessionStorage.removeItem(state.userIdKey)
            sessionStorage.removeItem(state.userInfoKey)
            sessionStorage.removeItem(state.currentActiveMenuKey)
            //清空横向菜单缓存
            sessionStorage.removeItem('hMenuListKey')
            sessionStorage.removeItem('hCurrentActiveMenuKey')
        }
    }

})

//------------------------Vuex配置缓存------------------------
